package leeon.bbsbrower.actions;

import ipworks.IPWorksException;

import java.util.ArrayList;

import leeon.bbsbrower.models.DocObject;
import leeon.bbsbrower.util.HTMLUtil;
import leeon.bbsbrower.util.HTTPBean;

public class BBSDocAction {
	
	public static final String URL_DOC_FROM_ONE_BOARD = Config.SERVER_URL + Config.SERVER_CGI + "bbsdoc";
	
	public static final String URL_DOC_FROM_ONE_BOARD_T = Config.SERVER_URL + Config.SERVER_CGI + "bbstdoc";
	
	/**
	 * 获取文章列表,from是null则从最新的开始
	 * @return
	 * @throws IPWorksException 
	 */
	public static DocObject[] getDocsByBoard(String board, String from) throws IPWorksException {
		return getDocsByURL(URL_DOC_FROM_ONE_BOARD + "?board=" + board, from);
	}
	
	/**
	 * 获取文章列表,from是null则从最新的开始
	 * @return
	 * @throws IPWorksException 
	 */
	public static DocObject[] getDocsByURL(String url, String from) throws IPWorksException {
		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		String content = http.get(url + (from==null?"":"&start="+from));
		content = HTMLUtil.findStr(content, "<table bgcolor=#ffffff width=100%>", "</table>");
		String[] rows = HTMLUtil.dealTableRows(content);
			
		ArrayList<DocObject> list = new ArrayList<DocObject>();
		if (rows.length > 1) {
			for (int i = 1; i < rows.length; i ++) {
				String[] cells = HTMLUtil.dealTableCells(rows[i]);
				String[] titles = dealDocTitle(cells[4]);
					
				DocObject doc = new DocObject(
					cells[0],
					HTMLUtil.removeHtmlTag(cells[1]).trim(),
					HTMLUtil.removeHtmlTag(cells[2]).trim(),
					cells[3],
					titles[1],
					titles[0]
				);
				list.add(doc);
			}
		}
			
		return list.toArray(new DocObject[0]);
	}
	
	/**
	 * 处理一般模式文章的标题，拆分成url和title
	 * @param source
	 * @return
	 */
	private static String[] dealDocTitle(String source) {
		if (source != null) {
			String[] re = new String[2];			
			
			//url
			String t = HTMLUtil.findStr(source, "<a href=", ">");
			if (t == null) return null;
			else re[0] = Config.SERVER_URL + Config.SERVER_CGI + t;
			
			//title
			t = HTMLUtil.findStr(source, "<font style='color:#000000'>", "</font>");
			if (t == null) t = HTMLUtil.findStr(source, "<img src=/images/types/text.gif align=absmiddle border=0>", "</a>");
			if (t == null) return null;
			else re[1] = t;
			
			
			//文章数
			t = HTMLUtil.findStr(source, "<font style='font-size:12px; color:#008080'>", "<");
			if (t != null) re[1] += "(" + t + ")";
			
			//re或者主题
			if (source.indexOf("/images/types/reply.gif") == -1)
				re[1] = "★  " + re[1];
			else
				re[1] = "Re: " + re[1];
			
			return re;			
		} else
			return null;
	}
	

	/**
	 * 获取主题模式文章列表,from是null则从最新的开始
	 * @return
	 * @throws IPWorksException 
	 */
	public static DocObject[] getTDocsByBoard(String board, String from) throws IPWorksException {
		return getTDocsByURL(URL_DOC_FROM_ONE_BOARD_T + "?board=" + board, from);
	}
	
	/**
	 * 获取主题模式文章列表,from是null则从最新的开始
	 * @return
	 * @throws IPWorksException 
	 */
	public static DocObject[] getTDocsByURL(String url, String from) throws IPWorksException {

		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		String content = http.get(url + (from==null?"":"&start="+from));
		content = HTMLUtil.findStr(content, "<table bgcolor=#ffffff width=100%>", "</table>");
		String[] rows = HTMLUtil.dealTableRows(content);
			
		ArrayList<DocObject> list = new ArrayList<DocObject>();
		if (rows.length > 1) {
			for (int i = 1; i < rows.length; i ++) {
				String[] cells = HTMLUtil.dealTableCells(rows[i]);
				String[] titles = dealTDocTitle(cells[4]);
				
				DocObject doc = new DocObject(
					cells[0],
					HTMLUtil.removeHtmlTag(cells[1]).trim(),
					HTMLUtil.removeHtmlTag(cells[2]).trim(),
					cells[3],
					titles[1],
					titles[0]
				);
				list.add(doc);
			}
		}
			
		return list.toArray(new DocObject[0]);
	}
	
	/**
	 * 从主题模式标题html中，分解出标题以及url
	 * @param source
	 * @return
	 */
	private static String[] dealTDocTitle(String source) {
		if (source != null) {
			int s = 0, e = 0;
			String[] re = new String[2];
			s = source.indexOf("<a href=");
			
			if (s != -1) s += 8;
			else return null;
			
			//url
			e = source.indexOf(">", s);
			if (e != -1) re[0] = source.substring(s, e);
			else return null;
			
			//title
			s = e + 1;
			e = source.indexOf("</a>", s);
			if (e != -1) re[1] = source.substring(s, e);
			else return null;
			
			//处理title			 
			re[1] = "★  " + re[1];			
			re[0] = Config.SERVER_URL + Config.SERVER_CGI + re[0];
			
			return re;
		} else
			return null;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}

}
